<template>
  <div>
    <el-button type="primary"
      icon="el-icon-plus"
      style="margin-bottom:10px"
      size="small"
      @click="handleAdd">新增</el-button>
    <el-table v-loading="loading"
      :data="orderList">
      <el-table-column label="功能名称"
        prop="name" />
      <el-table-column label="ID"
        prop="resource_id" />
      <el-table-column label="智币/次"
        show-overflow-tooltip=""
        prop="price">
      </el-table-column>
      <el-table-column label="单位"
        show-overflow-tooltip=""
        prop="cell_name">
      </el-table-column>
      <el-table-column label="操作"
        fixed="right"
        width="60"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small"
            type="text"
            @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="total>10"
      :total="total"
      layout="total,slot,sizes,prev,pager,next,jumper"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList">
    </pagination>
    <el-dialog title="功能定价" :visible.sync="open" width="500px" :close-on-click-modal="false">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="110px">
        <el-form-item
          label="功能"
          prop="resource_id">
          <el-select v-model="form.resource_id"  style="width:100%" placeholder="请选择" :disabled="!!form.id">
            <el-option v-for="item in payTypeOptions"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price"
            placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="单位" prop="cell_name">
          <el-input v-model="form.cell_name"
            placeholder="请输入单位"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getResourceList, addResource, updateResource } from '@/api/recharge-zb'
export default {
  name: 'ResourcePrice',
  data () {
    return {
      // 遮罩层
      loading: true,
      queryParams: {
        page: 1,
        page_size: 10
      },
      // 总条数
      total: 0,
      // 表格数据
      orderList: null,
      open: false,
      form: {
        id: null,
        resource_id: null,
        price: '',
        cell_name: ''
      },
      rules: {
        resource_id: [
          { required: true, message: '功能不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '价格不能为空', trigger: 'blur' }
        ],
        cell_name: [
          { required: true, message: '单位不能为空', trigger: 'blur' }
        ]
      },
      payTypeOptions: []
    }
  },
  created () {
    this.getDicts('resource_list').then(res => {
      this.payTypeOptions = res.data || []
    })
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      getResourceList(this.queryParams).then(res => {
        const data = res.data || {}
        this.orderList = data.list || []
      }).finally(() => {
        this.loading = false
      })
    },
    handleAdd () {
      this.resetForm('form')
      this.form.id = null
      this.open = true
    },
    handleEdit (row) {
      this.resetForm('form')
      this.open = true
      this.$nextTick(() => {
        this.form.id = 999
        Object.keys(this.form).forEach(k => {
          this.form[k] = row[k] ?? this.form[k]
        })
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.form)
          params.name = this.payTypeOptions.find(it => it.dictValue === params.resource_id)?.dictLabel || ''
          params.price = parseInt(params.price) || 0
          if (params.id) {
            delete params.id
            updateResource(params).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addResource(params).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
